package com.applink.sell_proof.utilXMLParser;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.content.Context;

import com.applink.sell_proof.share.Parameter;

public class ObjectFormatParser {
	
	public static ArrayList<ObjPropInfo> ParserIntoObjProp(Context paramContext, String xmlfilename, String tagname, String objectId) {

		ArrayList<ObjPropInfo> objPropInfoList = new ArrayList<ObjPropInfo> ();
		
        try {
        	
        	InputStream istream = paramContext.getAssets().open(xmlfilename);
            InputSource isource = new InputSource(new InputStreamReader(istream,  "UTF-8"));
            
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(isource);
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName(tagname);
            
            for (int temp = 0; temp < nList.getLength(); temp++) {
         
            	Node nNode = nList.item(temp);

            		if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                	
	                	ObjPropInfo objPropInfo = new ObjPropInfo();
	                	String content ="";
	                	Element element = (Element) nNode;
	                	String objClass = "";
	                	//get ID of this property
	                	NodeList nodes = element.getElementsByTagName(Parameter.OBJ_CLASS_ID);
	                	if (nodes.getLength() !=0){
	                		content = getTextNodeValue(nodes.item(0));
	                		objPropInfo.setOm(content); //done
	                		objClass = content ;
	                	}
	                	
	                	//get ID of this property
	                	nodes = element.getElementsByTagName(Parameter.OBJ_PROP_ID);
	                	if (nodes.getLength() !=0){
	                		content = getTextNodeValue(nodes.item(0));
	                		objPropInfo.setOPID(content); //done
	                	}
	                		 // check isKey, 
	                		 Node nNodeID  =nodes.item(0);
	                			Element elementchild = (Element) nNodeID ;
	                			String iskey= elementchild.getAttribute(Parameter.OBJ_PROP_ISKEY);
	                			objPropInfo.setIsKey(iskey);      //done     
	                		
	                		 //or (isFKey, fkeyID)
	                			String isfkey= elementchild.getAttribute(Parameter.OBJ_PROP_ISFKEY);
	                			objPropInfo.setIsFKey(isfkey);    //done     
	                			
	                			String fkeyid= elementchild.getAttribute(Parameter.OBJ_PROP_FKEYID);
	                			objPropInfo.setFKeyID(fkeyid);    //done        
	                		               		 
	            		//get datatype of this property
	                 	nodes = element.getElementsByTagName(Parameter.OBJ_PROP_DATATYPE);
	                 	if (nodes.getLength() !=0){
	                 		content = getTextNodeValue(nodes.item(0));                 
	                 		objPropInfo.setDataType(content); //done
	                 	}	 
	                	
	                	//get name of this property
	                 	nodes = element.getElementsByTagName(Parameter.OBJ_PROP_NAME);
	                 	if (nodes.getLength() !=0){
	                 		content = getTextNodeValue(nodes.item(0));
	                 		objPropInfo.setPropName(content); //done
	                 	}
	                 	
	                 	if(objClass.compareTo(objectId) == 0)
	                 		objPropInfoList.add(objPropInfo);
	                }
            }
        }catch (Exception e) {
            System.out.println("Catch");
            e.printStackTrace();
        }
        return objPropInfoList;
	}
	
	private static String getTextNodeValue(Node node) {
        Node child;
        if (node != null) {
            if (node.hasChildNodes()) {
                child = node.getFirstChild();
                while(child != null) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return "";
    }
}
